/**
 * @file    If_di.c
 * @brief   驱动接口-DI输入
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-25
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-25 <td>1.0.1     <td>name     <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "If_di.h"
#include "drv_gpio_pcfg.h"
#include "mcu_type.h"
#include "drv_gpio.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/
typedef struct 
{
    uint8_t is_init ; /* !< 是否初始化 TRUE 初始化 FALSE 不初始化*/
    uint8_t on_state ;   /* !< 开启时候状态 */
    GPIO_PORT port;
    GPIO_PIN pin;
}di_map_type;
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/
static di_map_type di_map_list[DI_ID_MAX] = 
{
    {TRUE , GPIO_RESET , CHG_SIGN_PORT , CHG_SIGN_PIN}  ,  /* 充电信号 */
    {TRUE , GPIO_RESET , LOAD_PORT  , LOAD_PIN  }  ,  /* 负载信号 */
    {TRUE , GPIO_RESET , ADDR1_PORT , ADDR1_PIN }  ,  /* ADDR1 */
    {TRUE , GPIO_RESET , ADDR2_PORT , ADDR2_PIN }   ,  /* ADDR2 */
    {TRUE , GPIO_RESET , ADDR3_PORT , ADDR3_PIN }  ,  /* ADDR3 */
    {TRUE , GPIO_RESET , ADDR4_PORT , ADDR4_PIN }  ,  /* ADDR4 */
    {TRUE , GPIO_RESET , ADDR5_PORT , ADDR5_PIN }  ,  /* ADDR5 */
    {TRUE , GPIO_RESET , ADDR6_PORT , ADDR6_PIN }  ,  /* ADDR6 */
    {TRUE , GPIO_RESET , RST_SING_PORT , RST_SING_PIN    }  ,  /* 按键信号 */
    {TRUE , GPIO_SET   , ADDR_DI_PORT   , ADDR_DI_PIN    }  ,  /* 自动寻址低 */
	{TRUE , GPIO_RESET , SW_PORT    , SW_PIN },
	{TRUE , GPIO_SET , RESEVER_PORT    , RESEVER_PIN },	
};
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief 初始化所有的di接口
 */
void If_di_init_all(void)
{
    uint8_t i = 0;
    for( i = 0;i < DI_ID_MAX; ++ i)
    {
        if((TRUE == di_map_list[i].is_init) && (NULL != di_map_list[i].port))
        {
            drv_gpio_cfg_mode(di_map_list[i].port , di_map_list[i].pin , GPIO_MODE_INPUT);
        }
    }
	
	// 防止外部误触发
	PH->PUSEL &= 0xffcfffff;
	PH->PUSEL |= 0x00100000;
	
	PB->PUSEL &= 0xfffffffc;
	PB->PUSEL |= 0x01;
}

/**
 * @brief  读取拨码地址
 * @return uint8_t 
 */
uint8_t If_di_read_addr(void)
{
    uint8_t addr = 0;

    if(GPIO_RESET == drv_gpio_read_pin(ADDR1_PORT , ADDR1_PIN))
       addr |= 0x01;
    if(GPIO_RESET == drv_gpio_read_pin(ADDR2_PORT , ADDR2_PIN))
       addr |= 0x02;
    if(GPIO_RESET == drv_gpio_read_pin(ADDR3_PORT , ADDR3_PIN))
       addr |= 0x04;
    if(GPIO_RESET == drv_gpio_read_pin(ADDR4_PORT , ADDR4_PIN))
       addr |= 0x08;
/*新增20241030*/	
#if CFG_PARALLEL_NUM > 16
	if(GPIO_RESET == drv_gpio_read_pin(ADDR5_PORT , ADDR5_PIN))
       addr |= 0x10;
	if(GPIO_RESET == drv_gpio_read_pin(ADDR6_PORT , ADDR6_PIN))
       addr |= 0x20;
#endif
	
    return addr;
}

di_type If_di_read_parallel_sw(void)
{
	if(GPIO_RESET == drv_gpio_read_pin(ADDR6_PORT , ADDR6_PIN))
       return DI_IN_ON;
	else
	   return DI_IN_OFF;
}

/**
 * @brief  读取DI输入状态 
 * @param  di_id
 * @return uint8_t 
 */
di_type If_di_read_in(uint8_t di_id)
{
    uint8_t state = 0;
    if(di_id >= DI_ID_MAX)
        return DI_IN_OFF;
    
    state = drv_gpio_read_pin(di_map_list[di_id].port , di_map_list[di_id].pin);

    if(di_map_list[di_id].on_state == state)
        return DI_IN_ON;
    else 
        return DI_IN_OFF;
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


